﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "../impl/head.h"
#include "export.h"

#if defined( _DEBUG )
#if defined( XOS_WIN32 )
#include "../../../../xos_lib/win32/vld/include/vld.h"
#elif defined( XOS_WIN64 )
#include "../../../../xos_lib/x64/vld/include/vld.h"
#endif
#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace xos_aio
{
    int create( xos_container::i_container * pContainer, i_aio_mgr ** ppv )
    {
        int ret = 0;

        aio_mgr::T * pObj = 0;

        if( ( 0 == ret ) && !ppv )
        {
            ret = 1;
        }

        if( ( 0 == ret ) && g_pSysData )
        {
            ret = 1;
        }

        if( 0 == ret )
        {
            g_pSysData = new sys_data( pContainer );
            if( !g_pSysData )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            ret = g_pSysData->init();
        }

        if( 0 == ret )
        {
            pObj = new aio_mgr::T;
            if( !pObj )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            ret = pObj->init();
        }

        if( 0 == ret )
        {
            *ppv = pObj;
            pObj = 0;
        }

        if( pObj )
        {
            xos_stl::release_interface( pObj );
            g_pSysData->term();
            delete g_pSysData;
            g_pSysData = 0;
        }

        return ret;
    }
} // xos_aio
